/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.whu.weibo_souhu.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.whu.weibo_souhu.login.Cookie;
import org.whu.weibo_souhu.login.LoginInfo;
import org.whu.weibo_souhu.util.URL;
import org.whu.weibo_souhu.login.SouhuLogin;
/**
 *
 * @author Administrator
 */
public class getweibo {
    
    public String html;
    public Integer num;
    public String keywords;
    public SouhuLogin souhulogin;
 //  public String url;
    public Cookie cookie;
    
    public getweibo(String username,String pwd) throws Exception
    { 
        LoginInfo logininfo=new LoginInfo(username,pwd);
        this.souhulogin=new SouhuLogin(logininfo);
        this.cookie=souhulogin.GetCookie();
       // this.url=url;
    }
    
    /**
     * 此函数用户组抓取网页内容
     * @return html 网页html内容，utf-8形式
     */ 
    public String getcontent(String url)
    {
        if(souhulogin.Connected())
        {
            this.html=new URL(url,cookie).gethtml();
        }
       return this.html;
    }
    
    /**
     * 此函数用于获得网页中搜索到的微博数目
     * @param patternstr 网页中匹配微博数目的句子
     * @param i 第几个子匹配
     * @return num 微博数目
     */
    public Integer getnum(String patternstr,int i)
    {
        Pattern pattern=Pattern.compile(patternstr);
        Matcher matcher=pattern.matcher(html);
        String temp=new String();
        if(matcher.find())
        {
            temp=matcher.group(i);
            if(temp.indexOf(",")!=-1)
                temp=temp.replaceAll(",", "");
           this.num=Integer.parseInt(temp);
        }
        else
            this.num=0;
        return this.num;
    }
    
    /**
     * 抓取微博中的所有信息
     * @param keywords 搜索关键词
     * @param timerange  搜索时间限定
     * @return result 关于此关键词在指定时间段的微博内容
     * @throws InterruptedException 
     */
    public ArrayList<String> getweibo(String keywords,String timerange) throws InterruptedException
    {
        ArrayList<String> result=new ArrayList<String>();
        Integer pagenum=1;
        String content;
        String url=new String();       
        url="http://t.sohu.com/twsearch/twSearch?timerange="+timerange+"&key="+keywords+"&timetype=custom&pageNo=1";
        String html=this.getcontent(url);
        String patternstr="<p class=\"page\"><em>共(.+?)页|</em>";
        pagenum=this.getnum(patternstr,1);
        for(int i=0;i<pagenum;i++)
        {
            url="http://t.sohu.com/twsearch/twSearch?timerange="+timerange+"&key="+keywords+"&timetype=custom&pageNo="+pagenum.toString();
            content=this.getcontent(url);
            if(content.equals(""))
                break;
            result.add(content);
            Thread.sleep(4000 + (int) (Math.random() * 5));
        }
        return result;
    }
    
    public ArrayList<String[]> htmltoweibo(String html)
    {
        ArrayList<String> weibo_content=new ArrayList<String>();
        ArrayList<String[]> result=new ArrayList<String[]>();
       //这个pattern只能抽取得到微博内容。
        Pattern pattern=Pattern.compile("(<p class=\"ugc\">)(<p class=\"ugc ugc2\">)<span class='em4'>"+keywords+"</span>.*</p>");
       //这个可获得微博内容和发布时间发布作者。
        Pattern pattern1=Pattern.compile("<div id=\"\\d{5,12}_con\" class=\"twi\">");
        Matcher matcher=pattern.matcher(html);
        String weibo_url=new String();
        while(matcher.find())
        {
            weibo_url=matcher.group();
            result.add(this.weibopro(weibo_url));
        }
        //关于删帖，具体还不知道搜狐是如何处理的。      
        return result;
    }
    
    public String[] weibopro(String weibo)
    {
        Integer N=3;
        String[] result=new String[N];
        String writer_url="<a title=\".*\" data-content='.*' href=\"http:.*\">(.*)</a>";
        String time_url="<b class=\"tm\" title=\"(.*)\">"; //这个写的不够严谨。后期再改。
        String content_url="(<p class=\"ugc\">)|(<p class=\"ugc ugc2\">)(.*)</p>";
        String[] weibo_url={content_url,time_url,writer_url};
        Pattern pattern;
        Matcher matcher;
        String temp;
        for(int i=0;i<weibo_url.length;i++)
        {
            pattern=Pattern.compile(weibo_url[i]);
            matcher=pattern.matcher(weibo);
            temp=matcher.group(1);
            if(i==0)
            {    
                temp=temp.replace("<span class='em4'>", "");
                temp=temp.replace("</span>", "");
            }
                
            if(matcher.find())
                result[i]=temp;
            else
                System.out.print("error");  
        }   
        return result;
    }
    
    
    
    
    
    
    
}
